package algorithm;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.time.Clock;
import java.util.Map;

import algorithm.User_level;
import DB.DB_connection_pool;

public class Point_record {

	public static final String login_event = "登录";
	public static final String publish_event = "发布动态";
	public static final String review_event = "评论";
	public static final String praise_event = "点赞";
	public static final String attention_event = "关注";
	public static final String forword_event = "转发";
	public static final String being_review_event = "被评论";
	public static final String being_praise_event = "被点赞";
	public static final String being_attention_event = "被关注";
	public static final String being_forword_event = "被转发";
	public static final String dynamic_active_event = "评论活跃";
	public static final String basketball_event = "篮球";
	public static final String swimming_event = "游泳";
	public static final String running_event = "跑步";
	public static final String exchange_of_goods = "商品兑换";
	public static final String system_penalty = "系统惩罚";
	public static final String personal_information = "信息完善";
	
	
	/*
	 * 积分记录
	 * user_account  用户账户
	 * event  事件类型
	 * change_point  积分变化多少
	 * change_tf  积分是否改变
	 * 
	 */
	public void point_record(String user_account ,String event ,double change_point ,boolean change_tf){
		if(change_tf == true){
         String insert_sql = "insert into point_record(point_record_id,user_account,point_record_time,point_record_event,point_lostget) values(?,?,?,?,?)";
		
			try {
				Clock clock = Clock.systemUTC();
				String point_record_id = String.valueOf((int)(Math.random()*900000)+100000)+clock.millis();  //记录编号
				java.sql.Date point_record_time = new java.sql.Date(clock.millis());     //当前时间
				
				Connection conn = DB_connection_pool.data_pool.getConnection();
				PreparedStatement ps_insert = conn.prepareStatement(insert_sql);
				ps_insert.setString(1, point_record_id);
				ps_insert.setString(2, user_account);
				ps_insert.setDate(3, point_record_time);
				ps_insert.setString(4, event);
				ps_insert.setDouble(5, change_point);
				ps_insert.executeUpdate();
				
				ps_insert.close();
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		
		}else{
			//do not
		}
	}
	
	/*
	 * 积分改变
	 * user_account  用户账户
	 * point  改变的积分
	 */
	public void change_point(String user_account ,double point){
		String update_credit = "update user_data set user_integral = user_integral + '"+point+"' where user_account='"+user_account+"'";
		
		try {
			Connection conn = DB_connection_pool.data_pool.getConnection();
			PreparedStatement int_update = conn.prepareStatement(update_credit);
			int_update.executeUpdate();
			int_update.close();
			
			//增加经验值
			if(point>0){
			String updata_user_experience = "update user_data set user_experience = user_experience + '"+point+"' where user_account='"+user_account+"'";
			PreparedStatement experience_update = conn.prepareStatement(updata_user_experience);
			experience_update.executeUpdate();
			experience_update.close();
			
			//查询当前经验值和等级
			Double experience = 0.0;
			int user_level = 0;
			String select_user_experience = "select * from user_data where user_account='"+user_account+"'";
			PreparedStatement experience_select = conn.prepareStatement(select_user_experience);
			ResultSet rs = experience_select.executeQuery();
			if(rs.next()){
				experience = rs.getDouble("user_experience");
				user_level = rs.getInt("user_level");
			}else{
				//do not
			}
			
			//判断是否升级
			User_level level = new User_level();
			Map<String,Integer> map = level.Get_user_level(experience, user_level);
			if(map.get("flag") == 1){    //升级了
				String updata_user_level = "update user_data set user_level = '"+map.get("user_level")+"'";
				PreparedStatement level_update = conn.prepareStatement(updata_user_level);
				level_update.executeUpdate();
				level_update.close();
			}else{
				//do not
			}
			
			}
            conn.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	
	/*
	 * 根据动态编号查询发布动态的用户，并统计数据判定是否加分
	 * 
	 * dynamic_information_id     动态编号
	 * being_type      事件类型
	 * 
	 */
	public void point_being_change(String dynamic_information_id ,String being_type){
		String user_account = "";
		int number = 0;   //被（点赞/关注/评论/转发）数
		String select_sql = "select * from dynamic_information where dynamic_information_id = '"+dynamic_information_id+"'";
		try {
			Connection conn = DB_connection_pool.data_pool.getConnection();
			PreparedStatement ps_select = conn.prepareStatement(select_sql);
			ResultSet rs = ps_select.executeQuery();
			if(rs.next()){
				user_account = rs.getString("user_account");
				if(being_type.equals(Point_record.being_attention_event)){     //被关注
					number = rs.getInt("dynamic_information_attention_number");
					Dynamic_credit dc = new Dynamic_credit();
					Map<String,Integer> map = dc.Get_being_credit(Dynamic_credit.attention, number);
					if(map.get("flag")==1){//积分改变
						
						change_point(user_account,(double)(map.get("credit")));
						point_record(user_account,Point_record.being_attention_event,(double)(map.get("credit")),true);
					}
				}else if(being_type.equals(Point_record.being_praise_event)){    //被点赞
					number = rs.getInt("dynamic_information_praise_number");
					Dynamic_credit dc = new Dynamic_credit();
					Map<String,Integer> map = dc.Get_being_credit(Dynamic_credit.praise, number);
                    if(map.get("flag")==1){//积分改变
						change_point(user_account,(double)(map.get("credit")));
						point_record(user_account,Point_record.being_praise_event,(double)(map.get("credit")),true);
					}
				}else if(being_type.equals(Point_record.being_review_event)){   //被评论
					number = rs.getInt("dynamic_information_review_number")-1;
					//System.out.println(number);
					Dynamic_credit dc = new Dynamic_credit();
					Map<String,Integer> map = dc.Get_being_credit(Dynamic_credit.review, number);
					//System.out.println((double)(map.get("credit")));
                    if(map.get("flag")==1){//积分改变
						change_point(user_account,(double)(map.get("credit")));
						point_record(user_account,Point_record.being_review_event,(double)(map.get("credit")),true);
					}
				}else if(being_type.equals(Point_record.being_forword_event)){   //被转发
					number = rs.getInt("dynamic_information_forword_number");
					Dynamic_credit dc = new Dynamic_credit();
					Map<String,Integer> map = dc.Get_being_credit(Dynamic_credit.forword, number);
                    if(map.get("flag")==1){//积分改变
						change_point(user_account,(double)(map.get("credit")));
						point_record(user_account,Point_record.being_forword_event,(double)(map.get("credit")),true);
					}
				}else{
					//非正常事件
				}
			}
			
			rs.close();
			ps_select.close();
			conn.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	
	/*
	 * 运动记录
	 * user_account  用户账户
	 * record  新的历史记录
	 * event 事件类型
	 */
	public void sports_record(String user_account , double record , String event){
		String update = "";
		if(event.equals(Point_record.basketball_event)){
			update = "update user_data set basketball_record = '"+record+"' where user_account = '"+user_account+"'";
		}else if(event.equals(Point_record.swimming_event)){
			update = "update user_data set swimming_record = '"+record+"' where user_account = '"+user_account+"'";
		}else if(event.equals(Point_record.running_event)){
			update = "update user_data set running_record = '"+record+"' where user_account = '"+user_account+"'";
		}else{
			//do not
		}
		
		
		try {
			Connection conn = DB_connection_pool.data_pool.getConnection();
			PreparedStatement level_update = conn.prepareStatement(update);
			level_update.executeUpdate();
			level_update.close();
			conn.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
	}
	
	
	
	
}
